﻿Imports System
Imports System.IO
Imports System.Text



Public Class TRV
    Inherits Connect

    Dim Func As Functions = New Functions

    Const TRV_MAX As Integer = 10

    Sub ReadShape()
        '読み込み用のReader
        Dim path As String = "TRVData.txt"
        Dim Reader As StreamReader = New StreamReader(path)
        Dim retval(10) As String
        Dim Read As String
        Dim StartingPoint, EndPoint As Esprit.Point
        Dim StartCoordinate(TRV_MAX, 2) As Double
        Dim EndCoordinate(TRV_MAX, 2) As Double
        Dim TRV_Num As Integer
        Dim ChainStart(TRV_MAX, 2), ChainEnd(TRV_MAX, 2) As Double
        Dim FeatureChainStartingPoint As Esprit.Point
        Dim Segments(10) As Esprit.Segment
        Dim flag As Boolean
        Dim OpenFace(TRV_MAX) As Boolean
        Dim EdgeNum As Integer
        Dim FeatureChain(TRV_MAX) As Esprit.FeatureChain
        Dim Key As String = ""
        Dim Depth As Double


        Read = Reader.ReadLine
        retval = Read.Split(CChar(","))
        TRV_Num = CInt(retval(0))

        For TRV_Counter = 0 To TRV_Num - 1
            Read = Reader.ReadLine
            retval = Read.Split(CChar(","))
            EdgeNum = CInt(retval(0))

            For EdgeCounter = 0 To EdgeNum - 1
                Read = Reader.ReadLine
                retval = Read.Split(CChar(","))

                StartCoordinate(EdgeCounter, 0) = CDbl(retval(0)) * 1000
                StartCoordinate(EdgeCounter, 1) = CDbl(retval(1)) * 1000
                StartCoordinate(EdgeCounter, 2) = CDbl(retval(2)) * 1000

                EndCoordinate(EdgeCounter, 0) = CDbl(retval(3)) * 1000
                EndCoordinate(EdgeCounter, 1) = CDbl(retval(4)) * 1000
                EndCoordinate(EdgeCounter, 2) = CDbl(retval(5)) * 1000
                OpenFace(EdgeCounter) = CBool(retval(6))
            Next

            Read = Reader.ReadLine
            retval = Read.Split(CChar(","))
            Depth = CDbl(retval(0))

            '最初の点を作成
            StartingPoint = Document.Points.Add(StartCoordinate(0, 0), StartCoordinate(0, 1), StartCoordinate(0, 2))
            EndPoint = Document.Points.Add(EndCoordinate(0, 0), EndCoordinate(0, 1), EndCoordinate(0, 2))

            ChainStart(0, 0) = StartCoordinate(0, 0)
            ChainStart(0, 1) = StartCoordinate(0, 1)
            ChainStart(0, 2) = StartCoordinate(0, 2)

            ChainEnd(0, 0) = EndCoordinate(0, 0)
            ChainEnd(0, 1) = EndCoordinate(0, 1)
            ChainEnd(0, 2) = EndCoordinate(0, 2)

            FeatureChainStartingPoint = StartingPoint
            Segments(0) = Func.CreateSegment(StartingPoint, EndPoint)


            'FeatureChainがStartとEndでつながるように並び替え
            For i = 0 To EdgeNum - 1
                flag = True
                For j = 0 To EdgeNum - 1
                    If ChainEnd(i, 0) = StartCoordinate(j, 0) And _
                        ChainEnd(i, 1) = StartCoordinate(j, 1) And _
                        ChainEnd(i, 2) = StartCoordinate(j, 2) Then
                        'i番目のStartとj番目のEndがつながる場合

                        If ChainStart(i, 0) = EndCoordinate(j, 0) And _
                        ChainStart(i, 1) = EndCoordinate(j, 1) And _
                        ChainStart(i, 2) = EndCoordinate(j, 2) Then Continue For

                        flag = False        'EndとEndがつながる可能性のflagをおる
                        StartingPoint = Document.Points.Add(StartCoordinate(j, 0), StartCoordinate(j, 1), StartCoordinate(j, 2))
                        EndPoint = Document.Points.Add(EndCoordinate(j, 0), EndCoordinate(j, 1), EndCoordinate(j, 2))

                        Segments(i + 1) = Func.CreateSegment(StartingPoint, EndPoint)

                        ChainStart(i + 1, 0) = StartCoordinate(j, 0)
                        ChainStart(i + 1, 1) = StartCoordinate(j, 1)
                        ChainStart(i + 1, 2) = StartCoordinate(j, 2)

                        ChainEnd(i + 1, 0) = EndCoordinate(j, 0)
                        ChainEnd(i + 1, 1) = EndCoordinate(j, 1)
                        ChainEnd(i + 1, 2) = EndCoordinate(j, 2)

                    End If
                Next
                'StartとEndで繋げない場合（EndとEndでつながる場合）
                If flag Then
                    For j = 0 To TRV_Num - 1
                        If ChainEnd(i, 0) = EndCoordinate(j, 0) And _
                            ChainEnd(i, 1) = EndCoordinate(j, 1) And _
                            ChainEnd(i, 2) = EndCoordinate(j, 2) Then
                            'i番目のEndとj番目のEndがつながる場合

                            If ChainStart(i, 0) = StartCoordinate(j, 0) And _
                            ChainStart(i, 1) = EndCoordinate(j, 1) And _
                            ChainStart(i, 2) = EndCoordinate(j, 2) Then Continue For

                            'Segmentの場合
                            EndPoint = Document.Points.Add(StartCoordinate(j, 0), StartCoordinate(j, 1), StartCoordinate(j, 2))
                            StartingPoint = Document.Points.Add(EndCoordinate(j, 0), EndCoordinate(j, 1), EndCoordinate(j, 2))

                            Segments(i + 1) = Func.CreateSegment(StartingPoint, EndPoint)

                            '***StartとEndを逆に入力***
                            ChainStart(i + 1, 0) = EndCoordinate(j, 0)
                            ChainStart(i + 1, 1) = EndCoordinate(j, 1)
                            ChainStart(i + 1, 2) = EndCoordinate(j, 2)

                            ChainEnd(i + 1, 0) = StartCoordinate(j, 0)
                            ChainEnd(i + 1, 1) = StartCoordinate(j, 1)
                            ChainEnd(i + 1, 2) = StartCoordinate(j, 2)

                        End If
                    Next
                End If
            Next
            FeatureChain(TRV_Counter) = Func.CreateFeatureChainSegment(EdgeNum, FeatureChainStartingPoint, Segments, Name, Key, Depth, True, False)

        Next


    End Sub







End Class
